<?php

class FriendsService {

	function areFriends($uId1, $uId2) {
		global $_SGLOBAL;
		$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('friend')."  WHERE uid='$uId1' AND fuid='$uId2' AND status='1'");
		$result = false;
		if($friend = $_SGLOBAL['db']->fetch_array($query)) {
			$result = true;
		}
		return $result;
	}

	function getByUId($uIds) {
		global $_SGLOBAL;
		$result = array();
		if (is_array($uIds)) {
			$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('friend')."  WHERE uid IN ('".implode("','", $uIds)."') AND status='1'");
		} else {
			$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('friend')."  WHERE uid = '". $uIds."' AND status='1'");
		}
		while($friend = $_SGLOBAL['db']->fetch_array($query)) {
			$result[$friend['uid']][] = $friend['fuid'];
		}
		return $result;
	}

	function get() {
		global $_SGLOBAL;
		$uId = $_SGLOBAL['supe_uid'];
		return $this->getByUId($uId);
	}

	function getRequest() {
		global $_SGLOBAL;
		$uId = $_SGLOBAL['supe_uid'];
		$notify = $result = array();
		$result = array(
			'friendRequest' => array(
				'uIds' => array()
			)
		);

		// 好友
		$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('friend')."  WHERE fuid='$uId' AND status='0' ORDER BY dateline DESC");
		$fIds = array();
		while($value = $_SGLOBAL['db']->fetch_array($query)) {
			if(!$result['friendRequest']['mostRecent']) {
				$result['friendRequest']['mostRecent'] = $value['dateline'];
			}
			$fIds[] = $value['uid'];
		}
		$result['friendRequest']['uIds'] = $fIds;

		return $result;
	}

}